﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  public sealed class FilterExpressionCollection : IList<FilterExpressionBase>, IList
  {
    #region Constructor

    internal FilterExpressionCollection( FilterExpressionOperator filterExpressionOperator )
    {
      if( filterExpressionOperator == null )
        throw new ArgumentNullException( "filterExpressionOperator" );

      m_parentFilterExpressionOperator = filterExpressionOperator;
    }

    #endregion

    internal void InternalAdd( FilterExpressionBase expression )
    {
      m_innerList.Add( expression );
    }

    #region IList<FilterExpressionBase> Members

    public int IndexOf( FilterExpressionBase item )
    {
      return m_innerList.IndexOf( item );
    }

    public void Insert( int index, FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Insert( index, item );
    }

    public void RemoveAt( int index )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.RemoveAt( index );
    }

    public FilterExpressionBase this[ int index ]
    {
      get
      {
        return m_innerList[ index ];
      }
      set
      {
        if( m_parentFilterExpressionOperator.IsFrozen )
          throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

        m_innerList[ index ] = value;
      }
    }

    #endregion

    #region ICollection<FilterExpressionBase> Members

    public void Add( FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Add( item );
    }

    public void Clear()
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      m_innerList.Clear();
    }

    public bool Contains( FilterExpressionBase item )
    {
      return m_innerList.Contains( item );
    }

    public void CopyTo( FilterExpressionBase[] array, int arrayIndex )
    {
      m_innerList.CopyTo( array, arrayIndex );
    }

    public int Count
    {
      get
      {
        return m_innerList.Count;
      }
    }

    public bool IsReadOnly
    {
      get
      {
        if( m_parentFilterExpressionOperator.IsFrozen )
          return true;

        return false;
      }
    }

    public bool Remove( FilterExpressionBase item )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      return m_innerList.Remove( item );
    }

    #endregion

    #region IEnumerable<FilterExpressionBase> Members

    public IEnumerator<FilterExpressionBase> GetEnumerator()
    {
      return m_innerList.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      return ( ( IEnumerable )m_innerList ).GetEnumerator();
    }

    #endregion

    #region IList Members

    int IList.Add( object value )
    {
      if( m_parentFilterExpressionOperator.IsFrozen )
        throw new InvalidOperationException( "An attempt was made to modify the collection while the FilterExpressionOperator is frozen." );

      return ( ( IList )m_innerList ).Add( value as FilterExpressionBase );
    }

    bool IList.Contains( object value )
    {
      return this.Contains( value as FilterExpressionBase );
    }

    int IList.IndexOf( object value )
    {
      return this.IndexOf( value as FilterExpressionBase );
    }

    void IList.Insert( int index, object value )
    {
      this.Insert( index, value as FilterExpressionBase );
    }

    bool IList.IsFixedSize
    {
      get
      {
        return ( ( IList )m_innerList ).IsFixedSize;
      }
    }

    void IList.Remove( object value )
    {
      this.Remove( value as FilterExpressionBase );
    }

    object IList.this[ int index ]
    {
      get
      {
        return this[ index ];
      }
      set
      {
        this[ index ] = value as FilterExpressionBase;
      }
    }

    #endregion

    #region ICollection Members

    void ICollection.CopyTo( Array array, int index )
    {
      throw new NotSupportedException( "ICollection.CopyTo() is not supported, use ICollection<FilterExpressionBase>.CopyTo() instead." );
    }


    bool ICollection.IsSynchronized
    {
      get
      {
        return ( ( ICollection )m_innerList ).IsSynchronized;
      }
    }

    object ICollection.SyncRoot
    {
      get
      {
        return ( ( ICollection )m_innerList ).SyncRoot;
      }
    }

    #endregion

    #region Private Fields

    private List<FilterExpressionBase> m_innerList = new List<FilterExpressionBase>();
    private FilterExpressionOperator m_parentFilterExpressionOperator; //null

    #endregion
  }
}
